package com.bluefish.bltuan.action.admin;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.annotation.Before;
import com.bluefish.bltuan.module.mail.EmailSetting;
import com.bluefish.bltuan.module.mail.Subscriber;
import com.bluefish.bltuan.service.EmailSettingService;
import com.bluefish.bltuan.service.SubscriberService;

@Results({
	@Result(name = "toIndex", type = "redirect", location = "/admin/email")
})
public class EmailAction extends AdminAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4211774828229179306L;

	private EmailSettingService emailSettingService;
	
	private SubscriberService subscriberService;
	
	private EmailSetting setting;

	private String name;

	private String username;

	private String password;

	private String smtp;

	private int smtpPort;

	private boolean smtpAuth;

	private boolean smtpTlsEnable;
	
	private boolean smtpTlsRequired;

	private int smtpSocketPort;

	private static final String SMTPSOCKETCLASS = "javax.net.ssl.SSLSocketFactory";

	private static final boolean SMTPSOCKETFALLBACK = false;

	private String subject;
	
	private List<Subscriber> subscribers;
	
	@Before(include = {"index", "update"})
	public void prepare(){
		this.setting = this.emailSettingService.getEmailSetting();
		if(this.setting == null)
			this.setting = new EmailSetting();
	}
	
	@Override
	public HttpHeaders create() {
		return null;
	}

	@Override
	public String destroy() {
		Subscriber subscriber = this.subscriberService.findById(getIntegerId());
		this.subscriberService.delete(subscriber);
		redirect();
		return null;
	}

	@Override
	public String edit() {
		return null;
	}

	@Override
	public HttpHeaders editNew() {
		return null;
	}

	@Override
	public HttpHeaders index() {
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}

	@Override
	public HttpHeaders show() {
		this.subscribers = this.subscriberService.list(getPageIndex());
		return new DefaultHttpHeaders(LIST).disableCaching();
	}

	@Override
	public String update() {
		this.setting.setName(name);
		this.setting.setUsername(username);
		this.setting.setPassword(password);
		this.setting.setSmtp(smtp);
		this.setting.setSmtpPort(smtpPort);
		this.setting.setSmtpAuth(smtpAuth);
		this.setting.setSmtpTlsEnable(smtpTlsEnable);
		this.setting.setSmtpTlsRequired(smtpTlsRequired);
		this.setting.setSmtpSocketClass(SMTPSOCKETCLASS);
		this.setting.setSmtpSocketFallback(SMTPSOCKETFALLBACK);
		this.setting.setSmtpSocketPort(smtpSocketPort);
		this.setting.setSubject(subject);
		this.emailSettingService.saveOrUpdate(this.setting);
		addFlashMessage(getText("update success!"));
		return TO_INDEX;
	}

	public void setEmailSettingService(EmailSettingService emailSettingService) {
		this.emailSettingService = emailSettingService;
	}
	
	public EmailSetting getSetting() {
		return setting;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setSmtp(String smtp) {
		this.smtp = smtp;
	}

	public void setSmtpAuth(boolean smtpAuth) {
		this.smtpAuth = smtpAuth;
	}

	public void setSmtpPort(int smtpPort) {
		this.smtpPort = smtpPort;
	}

	public void setSmtpSocketPort(int smtpSocketPort) {
		this.smtpSocketPort = smtpSocketPort;
	}

	public void setSmtpTlsEnable(boolean smtpTlsEnable) {
		this.smtpTlsEnable = smtpTlsEnable;
	}

	public void setSmtpTlsRequired(boolean smtpTlsRequired) {
		this.smtpTlsRequired = smtpTlsRequired;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	public List<Subscriber> getSubscribers() {
		return subscribers;
	}

	public void setSubscriberService(SubscriberService subscriberService) {
		this.subscriberService = subscriberService;
	}
}
